<?php
/**
 * *********************************************************************************************************************
 * 中通派件清单
 * *********************************************************************************************************************
 */

namespace soa\parter\zt;
use artisan\http;

class delivery
{
    /**
     * 服务地址
     * @var string
     */
    private $service_url = 'http://japi.zto.cn/zto/api_utf8/elkscanrecord';

    /**
     * 服务项目名称
     * @var string
     */
    private $service_name = 'kuaidihelp_zto';

    public function getServiceUrl()
    {
        return $this->service_url;
    }

    public function getServiceName()
    {
        return $this->service_name;
    }

    /**
     * @param $branchId string 网点id
     * @param $empNo string 业务员工号
     * @param $beginDate string 开始日期
     * @param $endDate string 结束日期
     * @return array|mixed
     */
    public function getDeliveryList($branchId, $empNo, $beginDate, $endDate)
    {
        if(empty($branchId) || empty($empNo))
            return array();

        //兼容原来接口返回数据格式
        $retData=[
            'billCodes'=>[],
            'billInfos'=>[]
        ];

        $beginDate = $beginDate ? date('Y-m-d H:i:s', strtotime($beginDate)) : date('Y-m-d 00:00:00');
        $endDate = $endDate ? date('Y-m-d H:i:s', strtotime($endDate)) : date('Y-m-d 23:59:59');

        set_time_limit(0);
        (mb_internal_encoding('UTF-8'));

        $bills = [];
        $billInfos=[];
        $total_page = 5; //默认取5页
        $time1 = time();
        for ($pageNum=1, $pageSize=1500;$pageNum <= $total_page;$pageNum++) {//修复中通派件数据不准确问题，中通分页逻辑有bug
            $data = '{"end_scan_date":"'.$endDate.'","pageIndex":'.$pageNum.',"pageSize":'.$pageSize.',"scan_site_id":'.
                $branchId.',"start_scan_date":"'.$beginDate.'","disp_or_send_man_code":"'.$empNo.'"}';

            $data = mb_convert_encoding($data, 'GBK');
            $post = array(
                'data' => $data,
                'msg_type' => 'disp',
                'data_digest' => base64_encode(md5($data . 'fde099cd2cf60738d765c48afb252a58', true)),
                'company_id' => "Kuaibao",
            );


            $options = [
                'HTTPHEADER' => ["Content-Type: application/x-www-form-urlencoded;charset=GBK"],
                'TIMEOUT' => 5
            ];

//中通返回派件列表数据:{"data":[{"billCode":"404782304560","registerDate":"2016-06-20 07:57:20"},{"billCode":"404776701269","registerDate":"2016-06-20 07:57:20"},{"billCode":"404762539794","registerDate":"2016-06-20 07:55:20"},{"billCode":"404691847626","registerDate":"2016-06-20 07:47:20"},{"billCode":"404821728515","registerDate":"2016-06-20 14:23:20"},{"billCode":"404785334721","registerDate":"2016-06-20 14:24:20"},{"billCode":"404741722796","registerDate":"2016-06-20 14:24:20"},{"billCode":"404783986189","registerDate":"2016-06-20 14:25:20"},{"billCode":"404711984289","registerDate":"2016-06-20 14:26:20"},{"billCode":"404853312685","registerDate":"2016-06-20 14:26:20"},{"billCode":"404861643516","registerDate":"2016-06-20 14:25:20"},{"billCode":"404827667233","registerDate":"2016-06-20 14:26:20"}]}
//$result=json_decode('{"data":[{"billCode":"404782304560","registerDate":"2016-06-20 07:57:20"},{"billCode":"404776701269","registerDate":"2016-06-20 07:57:20"},{"billCode":"404762539794","registerDate":"2016-06-20 07:55:20"},{"billCode":"404691847626","registerDate":"2016-06-20 07:47:20"},{"billCode":"404821728515","registerDate":"2016-06-20 14:23:20"},{"billCode":"404785334721","registerDate":"2016-06-20 14:24:20"},{"billCode":"404741722796","registerDate":"2016-06-20 14:24:20"},{"billCode":"404783986189","registerDate":"2016-06-20 14:25:20"},{"billCode":"404711984289","registerDate":"2016-06-20 14:26:20"},{"billCode":"404853312685","registerDate":"2016-06-20 14:26:20"},{"billCode":"404861643516","registerDate":"2016-06-20 14:25:20"},{"billCode":"404827667233","registerDate":"2016-06-20 14:26:20"}]}'
//,true);//测试用数据
            $result = http::post($this->getServiceUrl(), $post, $options);
            //如果失败了再次尝试一次
            if(empty($result) || $result == 'S04') {
                $result = http::post($this->getServiceUrl(), $post, $options);
                if(empty($result) || $result == 'S04')
                    continue;
            }
//logDebug('调试:中通派件返回数据格式:','zt_delivery_list_debug');
//logDebug($result,'zt_delivery_list_debug');

            $result = json_decode($result, true);

            if(json_last_error() !=0)
				continue;
            
            if($result['records'] <=0)
                break;
            
            $total_page = $result['total'];

            //$countDump=[];
            foreach ($result['data'] as $item) {
                //if(isset($billInfos[$item['billCode']])){
                //    $countDump[]=$item['billCode'];
                //}
                $bills[] = $item['billCode'];
                //保存 单号=>(单号,时间)信息
                $billInfos[$item['billCode']] = [
                    'billCode' => $item['billCode'],
                    'scanTime' => date('Y-m-d H:i:s',substr($item['scanDate'],0,10)),
                ];
            }            
        }

        $retData['billCodes']=$bills;
        $retData['billInfos']=$billInfos;

        //logDebug('重单个数:'.count($countDump).' 重单号:'.json_encode($countDump));
        //logDebug('bills:'.json_encode($bills));
//        logDebug('billInfos:'.json_encode($billInfos));

        logDebug(count($bills).' '.count($billInfos), 'delivery', (time()-$time1).' zto '.$empNo);
        return $retData;
    }
}